package restructure;

import java.util.Enumeration;
import java.util.Vector;

/**
 * @Description
 * @Date 2021/9/16
 * @Created by Jonathan
 */
public class Customer {
    private String _name;
    private Vector _rentals = new Vector();

    public Customer(String name) {
        _name = name;
    }

    public void addRental(Rental arg) {
        _rentals.add(arg);
    }

    public String getName() {
        return _name;
    }

    /**
     * 实际上就是遍历租赁的那个列表
     * 统计出总价和总积分
     *
     * @return 总价和租赁详情都在里面
     */
    public String statement() {
        //double totalAmount = 0;
        //double frequentRenterPoints = 0;
        Enumeration rentals = _rentals.elements();
        String result = "Rental Record for" + getName() + "\n";
        while (rentals.hasMoreElements()) {
            Rental each = (Rental) rentals.nextElement();
            //double thisAmount = each.getCharge();
            //frequentRenterPoints += each.getFrequentRenterPoints();
            // TODO: 2021/9/19  each.getCharge() 这个方法计算了两遍 有性能损耗 后面还回优化
            // show figures for the rental
            result += "\t" + each.getMovie().getTitle() + "\t" + each.getCharge() + "\n";
            // totalAmount += each.getCharge();
        }
        // add footer lines
        result += "Amount owed is" + getTotalCharge() + "\n";
        result += "you earned " + getTotalFrequentRenterPoints() + " frequent renter points";
        return result;
    }

    /**
     * @return todo 有个疑问 这个单独提出来再遍历一遍真的好嘛？？？
     */
    private double getTotalCharge() {
        double result = 0;
        Enumeration rentals = _rentals.elements();
        while (rentals.hasMoreElements()) {
            Rental each = (Rental) rentals.nextElement();
            result += each.getCharge();
        }
        return result;
    }

    /**
     * @return todo 有个疑问 这个单独提出来再遍历一遍真的好嘛？？？
     */
    private double getTotalFrequentRenterPoints() {
        double result = 0;
        Enumeration rentals = _rentals.elements();
        while (rentals.hasMoreElements()) {
            Rental each = (Rental) rentals.nextElement();
            result += each.getFrequentRenterPoints();
        }
        return result;
    }
}
